package com.tiger.log.server.service.impl;


import com.tiger.log.common.constant.LogConstant;
import com.tiger.log.common.constant.SaveProperties;
import com.tiger.log.common.entity.SysAuthLog;
import com.tiger.log.server.mapper.SysAuthLogMapper;
import com.tiger.log.server.service.SysAuthLogService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;


/**
 * @ClassName SysAuthLogImpl
 * @Description TODO
 * @Author huxuehao
 **/
@Service
public class SysAuthLogServiceImpl implements SysAuthLogService {
    private final String TABLE_NAME = "sys_auth_log";
    private final SysAuthLogMapper sysAuthLogMapper;
    private final SaveProperties logProperties;

    public SysAuthLogServiceImpl(SysAuthLogMapper sysAuthLogMapper, SaveProperties logProperties) {
        this.sysAuthLogMapper = sysAuthLogMapper;
        this.logProperties = logProperties;
    }
    @Override
    public List<SysAuthLog> getAll() {
        return sysAuthLogMapper.selectAll();
    }

    @Override
    public SysAuthLog getById(String id) {
        return sysAuthLogMapper.selectById(id);
    }

    @Override
    public List<SysAuthLog> getByUserCode(String userCode) {
        return sysAuthLogMapper.selectByUserCode(userCode);
    }

    @Override
    public int deleteAll() {
        return sysAuthLogMapper.deleteAll();
    }

    @Override
    public int deleteByIds(List<String> ids) {
        return sysAuthLogMapper.deleteByIds(ids);
    }

    @Override
    public int deleteByUserCodes(List<String> userCodes) {
        return deleteByUserCodes(userCodes);
    }

    @Override
    @Async("logCustomThreadPoolExecutor")
    public Boolean asyncGenLog(SysAuthLog authLog) {
        if (validAuthLog(authLog)) {
            return sysAuthLogMapper.add(authLog) > 0 ? true : false;
        }
        return false;
    }

    @Override
    public Boolean validDayCycle() throws ParseException {
        String dayCycleStr = sysAuthLogMapper.selectDayCycle();
        if (dayCycleStr == null || "".equals(dayCycleStr)) {
            return false;
        }
        String[] split = dayCycleStr.split(",");
        if (split[0].equals(split[1])) {
            return false;
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat(LogConstant.TIME_FORMAT);
        long minTime = dateFormat.parse(split[0]).getTime();
        long maxTime = dateFormat.parse(split[1]).getTime();

        return maxTime - minTime >= 1000 * 60 * 60 * 24 * logProperties.getSaveDays() ? true : false;
    }

    @Override
    public Boolean doRotate(String tableSuffix) throws ParseException {
        if (this.validDayCycle()) {
            sysAuthLogMapper.copyTable(TABLE_NAME + tableSuffix);
            sysAuthLogMapper.delRecordsIFExist(TABLE_NAME + tableSuffix);
            return true;
        }
        return false;
    }

    /* 验证AuthLog是否合法 */
    private boolean validAuthLog(SysAuthLog authLog) {
        return authLog.getUserCode() != null;
    }
}
